package com.egao.api.controller.nont;


import cn.hutool.core.util.StrUtil;
import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.damon.utils.CacheUtils;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import com.egao.common.system.service.OperRecordService;
import com.egao.common.system.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * api登录相关接口
 * Created by wangfan on 2019-05-29 下午 3:43.
 * http://localhost/swagger-ui.html
 */
@RequestMapping("/api/nt/user")
@RestController
@Api(value = "用户API",tags = {"用户API"})
public class ApiUserNtController extends BaseController {

    @Autowired
    private UserService userService;
    @Autowired
    private OperRecordService operRecordService;

    /**
     * 密码重置
     */
    @ApiOperation(value = "密码重置",notes = "" +
            "phoneNumber: 手机号码 必填\n" +
            "newPsw 新密码 必填\n" +
            "random 验证码 必填\n" +
            "",httpMethod = "POST")
    @PostMapping("/psw/reset")
    public JsonResult reset(String phoneNumber, String newPsw, String random, HttpServletRequest request) {
        if (StrUtil.hasBlank( phoneNumber, newPsw, random)) {
            return JsonResult.error("参数不能为空!");
        }

        User user = new User();
        user.setUsername(phoneNumber);

        try {
            user = userService.getUserByUsername(user);
        } catch (Exception e) {
            return JsonResult.error("没有该账号!");
        }
        user.setPasswordorigin(newPsw);
        user.setPassword(userService.encodePsw(newPsw));
//        HttpSession session = request.getSession(true);
//        String random2 = (String)session.getAttribute(user.getPhone());
        // 1. 创建缓存管理器
//        CacheManager cacheManager = CacheManager.create("./src/main/resources/ehcache/ehcache-shiro.xml");
       /* CacheManager cacheManager = CacheManager.create("d:/ehcache/ehcache-shiro.xml");
        // 2. 获取缓存对象
        Cache cache = cacheManager.getCache(CacheConst.PHONEMESSAGEVERIFY);
        // 5. 获取缓存
        Element value = cache.get(phoneNumber);*/
//        System.out.println(value);
//        System.out.println(value.getOb*jectValue());
//        String random2 = (String) value.getObjectValue();
        String random2 = CacheUtils.timedCache.get(phoneNumber);


        if (!random.equals(random2)) {
            return JsonResult.error("验证码不正确");
        }
        return userService.updatePsw(user, user);
    }



    /**
     * 拼箱系统 邮箱或者手机号注册
     */
    @OperLog(value = "邮箱或者手机号注册 假网站", desc = "邮箱或者手机号注册 假网站", result = true)
    @PostMapping("/pxRegister")
    @ApiOperation(value = "拼箱系统 邮箱或者手机号注册 假网站",notes = "" +
            //"organization_id int 组织id 默认值:4\n" +
            "username varchar 账号 必填\n" +
            "nickName varchar 姓名 必填\n" +
            "password varchar 密码 必填\n" +
            "phone varchar 手机号 \n" +
            "email varchar email \n" +
//            "roleIds 角色id\n" +
//            "例子:{\"customerId\":\"1\",\"username\":\"aaaa\",\"password\":\"123123\",\"phone\":\"13186850891\"}" +
            "",httpMethod = "POST")
    public JsonResult pxRegister(User user, HttpServletRequest request) {
        try {
            return userService.pxRegister(user);
        } catch (Exception e) {
            return JsonResult.error("用户名重复");
        }
    }

}
